package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.Entity.SetMeal;
import com.sky.Entity.SetMealDishs;
import com.sky.constant.StatusConstant;
import com.sky.dto.AddSetMealDTO;
import com.sky.dto.QuerySetmealByPageDTo;
import com.sky.mapper.SetmealDishsMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.service.SetmealService;
import com.sky.vo.PageVo;
import com.sky.vo.SetmealDishVo;
import com.sky.vo.SetmealVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishsMapper setmealDishsMapper;


    @Override
    @Transactional
    public void add(AddSetMealDTO addSetMealDTO) {
        SetMeal setMeal=new SetMeal();
        setMeal.setStatus(StatusConstant.DISABLE);
        BeanUtils.copyProperties(addSetMealDTO,setMeal);
        setmealMapper.insert(setMeal);
        Long mealId = setMeal.getId();
        log.info("保存的setmeal id="+ mealId);
        setmealDishsMapper.insert(mealId,addSetMealDTO.getSetmealDishes());
    }

    @Override
    public PageVo<SetmealVo> queryByPage(QuerySetmealByPageDTo querySetmealByPageDTo) {
        PageHelper.startPage(querySetmealByPageDTo.getPage(),querySetmealByPageDTo.getPageSize());
        Page<SetmealVo> page =setmealMapper.queryByPage(querySetmealByPageDTo);
        PageVo<SetmealVo> vo=new PageVo<>();
        vo.setRecords(page.getResult());
        vo.setTotal(page.getTotal());
        return vo;
    }

    @Override
    @Transactional
    public void update(AddSetMealDTO addSetMealDTO) {
        SetMeal setMeal=new SetMeal();
        BeanUtils.copyProperties(addSetMealDTO,setMeal);
        setmealMapper.update(setMeal);

        setmealDishsMapper.deleteBySetmealId(setMeal.getId());
        setmealDishsMapper.insert(setMeal.getId(),addSetMealDTO.getSetmealDishes());
    }

    @Override
    public SetmealVo queryById(Long id) {
        SetmealVo setMeal=setmealMapper.queryById(id);
        if (setMeal == null) {
            return null;
        }
        List<SetMealDishs> list=setmealDishsMapper.queryBySetmealId(id);
        setMeal.setSetmealDishes(list);
        return setMeal;
    }

    @Override
    public void setStatus(Integer status, Long id) {
        SetMeal setMeal=new SetMeal();
        setMeal.setStatus(status);
        setMeal.setId(id);
        setmealMapper.update(setMeal);
    }

    @Override
    public void delete(List<Long> ids) {
        if (ids == null||ids.size()==0) {
            return;
        }
        setmealMapper.delete(ids);
        setmealDishsMapper.deleteBySetmealIds(ids);
    }

    @Override
    public List<SetMeal> queryByCategoryId(Long categoryId) {
        return setmealMapper.queryByCategoryId(categoryId);
    }

    @Override
    public List<SetmealDishVo> queryDishsBySetmealId(Long id) {
        return setmealDishsMapper.queryDishsBySetmealId(id);
    }
}
